//
//  WLMMainTabBarController.swift
//  WeiBo
//
//  Created by Eric on 2017-01-04.
//  Copyright © 2017 Eric. All rights reserved.
//

import UIKit

class WLMMainTabBarController: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let wlmTabBar = WLMTabBar()
        
        wlmTabBar.composeClosure = { [weak self] in
            let composeView = WLMComposeView()
            composeView.show(fromVC: self)
        }
        
        self.setValue(wlmTabBar, forKey: "tabBar")
        
        addChildViewController()
    }
    
    private func addChildViewController() {
        addChildViewController(viewController: WLMHomeTableViewController(), title: "首页", imageName: "tabbar_home", index: 0)
        addChildViewController(viewController: WLMMessageTableViewController(), title: "消息", imageName: "tabbar_message_center", index: 1)
        addChildViewController(viewController: WLMDiscoverTableViewController(), title: "发现", imageName: "tabbar_discover",index: 2)
        addChildViewController(viewController: WLMProfileTableViewController(), title: "我", imageName: "tabbar_profile", index: 3)

    }
    

    private func addChildViewController(viewController:UIViewController, title:String, imageName:String ,index: Int) {
        
        viewController.title = title
        viewController.tabBarItem.tag = index
        viewController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orange], for: .selected)
        viewController.tabBarItem.image = UIImage(named: imageName)?.withRenderingMode(.alwaysOriginal)
        viewController.tabBarItem.selectedImage = UIImage(named: "\(imageName)_selected")?.withRenderingMode(.alwaysOriginal)
        viewController.tabBarItem.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -3)
        //用导航控制包起来
        let naviController = WLMBaseNavitionController(rootViewController: viewController)
        addChildViewController(naviController)
    }
    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        
        var index = 0
        for subView in tabBar.subviews {
            if subView.isKind(of: NSClassFromString("UITabBarButton")!) {
                if index == item.tag {
                    //可以获取到被点击tabbarButton
                    //遍历子视图
                    for v in subView.subviews {
                        if v.isKind(of: NSClassFromString("UIImageView")!) {
                            //线缩小
                            v.transform = CGAffineTransform.init(scaleX: 0.6, y: 0.6)
                            UIView.animate(withDuration: 0.6, delay: 0, usingSpringWithDamping: 0.3, initialSpringVelocity: 6, options: [], animations: {
                                v.transform = CGAffineTransform.identity
                            }, completion: { (_) in
                                
                            })
                        }
                    }
                }
                index += 1
            }
        }
    }
}
